﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Models;
using App.Views.ViewModels;
using App.Models.Pager;

namespace App.Controllers
{
    public class RefriCoffinInfoController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(int? page, int? rows, string order)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;
            PagedList<tb_RefriCoffinInfo> list = null;
            if ("asc" == order)
            {
                list = dbe.tb_RefriCoffinInfo.OrderBy(m => m.CoffinNO)
                                .ToPagedList(page ?? 1, rows ?? 10);
            }
            else
            {
                list = dbe.tb_RefriCoffinInfo.OrderByDescending(m => m.CoffinNO)
                                .ToPagedList(page ?? 1, rows ?? 10);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Create(RefriCoffinInfoViewModel vmodel)
        {
            try
            {
                var model = new tb_RefriCoffinInfo
                {
                    CoffinNO = vmodel.CoffinNO,
                    CoffinModel = vmodel.CoffinModel,
                    Remark = vmodel.Remark
                };
                dbe.tb_RefriCoffinInfo.AddObject(model);
                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                var res = new { success = false, msg = "保存出错！" };
                return Json(res, JsonRequestBehavior.AllowGet);
            }            
        }

        [HttpPost]
        public ActionResult Edit(string id, RefriCoffinInfoViewModel vmodel)
        {
            try
            {
                var model = dbe.tb_RefriCoffinInfo.FirstOrDefault(m => m.CoffinNO == id);
                model.CoffinModel = vmodel.CoffinModel;
                model.Remark = vmodel.Remark;
                dbe.SaveChanges();                
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                var res = new { success = false, msg = "保存出错！" };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
        }       
                 
        public ActionResult Delete(string ids)
        {
            try
            {
                string[] idList = ids.Split('&');
                foreach (var id in idList)
                {
                    var obj = dbe.tb_RefriCoffinInfo.FirstOrDefault(m => m.CoffinNO == id);
                    dbe.tb_RefriCoffinInfo.DeleteObject(obj);
                }

                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                var res = new { success = false, msg = "删除出错！" };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
